﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
namespace LotteryAdmin
{
    public partial class LotteryBonus : UserControl
    {
        private LotteryAdminClient srv;
        private int cbxReverseIndex = 0;
        public LotteryBonus()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LotteryBonus_Loaded);
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }
        void LotteryBonus_Loaded(object sender, RoutedEventArgs e)
        {
            InitComboBox();
            
        }
        void InitComboBox()
        {
            srv = WcfProxy.GetProxy;
            srv.GetAllLotteryInfoCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("无彩种数据,该页面无法使用!");
                    btnSearch.IsEnabled = false;
                    tabItem0.IsEnabled = false;
                    tabItem1.IsEnabled = false;
                    return;
                }
                List<LotteryInfo> list = er.Result.ToList();
                list.Insert(0, new LotteryInfo() {
                     LotteryTypeID=0, LotteryName="*[正向]",
                     Orientation=0
                });
                var c = list.Where(p => p.Orientation == 1).FirstOrDefault();
                list.Insert(list.IndexOf(c), new LotteryInfo()
                {
                    LotteryTypeID = 0,
                    LotteryName = "*[逆向]",
                    Orientation = 1
                });
                this.cbxLottery.ItemsSource = list;
                this.cbxLottery.DisplayMemberPath = "LotteryName";
                this.cbxLottery.SelectedValuePath = "LotteryTypeID";
                this.cbxLottery.Tag = list;
                this.cbxLottery.SelectedIndex = 0;
                cbxReverseIndex = list.IndexOf(c) - 1;
                cbxLottery.DropDownOpened += (se, erg) =>
                {
                    this.cbxLottery.UpdateLayout();
                    this.cbxLottery.ItemContainerGenerator.ContainerFromIndex(0).SetValue(
                        ComboBoxItem.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                    this.cbxLottery.ItemContainerGenerator.ContainerFromIndex(list.IndexOf(c)-1)
                        .SetValue(ComboBoxItem.ForegroundProperty, new SolidColorBrush(Colors.Red));
                };
                LoadPositive();
            };
            loadingProgress.IsBusy = true;
            srv.GetAllLotteryInfoAsync();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateName(this.txtPlayType.Text))
            {
                MessageBox.Show("玩法名称只能输入中文、英文、数字、下划线");
                return;
            }
            if (!Validate.ValidateName(this.txtPlayRadio.Text))
            {
                MessageBox.Show("玩法单选名称只能输入中文、英文、数字、下划线");
                return;
            }
            LoadData();
        }
        void LoadData()
        {
            var c = (this.cbxLottery.Tag as List<LotteryInfo>).Where
                (p => p.LotteryName == (cbxLottery.SelectedItem as LotteryInfo).LotteryName).FirstOrDefault();
            if (c.Orientation == 0)
            {
                tabItem0.IsSelected = true;
                LoadPositive();
            }
            else
            {
                tabItem1.IsSelected = true;
                LoadReverse();
            }
        }
        void LoadPositive()
        {
            tabItem0.IsSelected = true;
            srv = WcfProxy.GetProxy;
            srv.GetPlayTypeRadioForBonusCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayType_P.ItemsSource = er.Result;
            };
            string lotteryId=string.Empty;
            if ((cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID == 0)
                lotteryId = "";
            else
                lotteryId = (cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID.ToString();
            srv.GetPlayTypeRadioForBonusAsync(lotteryId, txtPlayType.Text.Trim(), 0, txtPlayRadio.Text.Trim());
            loadingProgress.IsBusy = true;
        }
        void LoadReverse()
        {
            if (rb_dftOdds.IsChecked == false && rb_cutOdds.IsChecked == false)
                rb_dftOdds.IsChecked = true;
            tabItem1.IsSelected = true;
            srv = WcfProxy.GetProxy;
            srv.GetDefaultOddsCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayType_R.ItemsSource = er.Result;
            };
            srv.GetCurrentOddsCompleted += (s, er) =>
            {
                loadingProgress.IsBusy = false;
                if (er.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                this.dgPlayType_R.ItemsSource = er.Result;
            };
            string lotteryId = string.Empty;
            if ((cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID == 0)
                lotteryId = "";
            else
                lotteryId = (cbxLottery.SelectedItem as LotteryInfo).LotteryTypeID.ToString();
            if(rb_dftOdds.IsChecked==true)
                srv.GetDefaultOddsAsync(lotteryId, txtPlayType.Text.Trim(), txtPlayRadio.Text.Trim());
            else
                srv.GetCurrentOddsAsync(lotteryId, txtPlayType.Text.Trim(), txtPlayRadio.Text.Trim());
            loadingProgress.IsBusy = true;
        }

        private void btChange_P_Click(object sender, RoutedEventArgs e)
        {
            ChangeBonus cb = new ChangeBonus((sender as Button).Tag as PlayTypeRadioInfo);
            cb.Show();
        }

        private void btChange_R_Click(object sender, RoutedEventArgs e)
        {
            ChangeOdds co = new ChangeOdds();
            if (rb_dftOdds.IsChecked==true)
                co.ShowDefaultOdds((sender as Button).Tag as Odds);
            else
                co.ShowCurrentOdds((sender as Button).Tag as Odds);
        }

        private void dgPlayType_P_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (dgPlayType_P.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex() + 1).ToString();
            (dgPlayType_P.Columns[6].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
            PlayTypeRadioInfo playType =e.Row.DataContext as PlayTypeRadioInfo;
            if (playType.Multiple == 1000)
                (dgPlayType_P.Columns[5].GetCellContent(e.Row) as TextBlock).Text = "固定返点";
            else
                (dgPlayType_P.Columns[5].GetCellContent(e.Row) as TextBlock).Text = (e.Row.DataContext as PlayTypeRadioInfo).MinScale.ToString();
        }

        private void dgPlayType_R_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (dgPlayType_R.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (e.Row.GetIndex() + 1).ToString();
            (dgPlayType_R.Columns[8].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            StackPanel sp = (sender as RadioButton).Parent as StackPanel;
            foreach (RadioButton rb in sp.Children)
            {
                if (rb != (sender as RadioButton))
                {
                    rb.Foreground = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    rb.Foreground = new SolidColorBrush(Colors.Red);
                }
            }
            if ((sender as RadioButton).Tag.ToString() == "0")
            {
                this.dgPlayType_R.Columns[7].Visibility = Visibility.Collapsed;
                this.dgPlayType_R.Columns[6].Visibility = Visibility.Visible;
                this.dgPlayType_R.Columns[5].Header = "默认赔率";
               
            }
            else
            {
                this.dgPlayType_R.Columns[7].Visibility = Visibility.Visible;
                this.dgPlayType_R.Columns[6].Visibility = Visibility.Collapsed;
                this.dgPlayType_R.Columns[5].Header = "当前期赔率";
            }
            LoadReverse();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((sender as TabControl).SelectedItem == tabItem0)
            {
                if (cbxLottery.SelectedIndex >= cbxReverseIndex)
                    cbxLottery.SelectedIndex = 0;
                LoadPositive();
            }
            if ((sender as TabControl).SelectedItem == tabItem1)
            {
                if (cbxLottery.SelectedIndex < cbxReverseIndex)
                    cbxLottery.SelectedIndex = cbxReverseIndex;
                LoadReverse();
            }
        }


    }
}
